﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//using FAP.GUI;
using FAP.DataAccess;
using FAP.DataAccess.BLL;

namespace FAP_GUI.UC
{
    public partial class ucDanhMuc : UserControl
    {
        public ucDanhMuc()
        {
            InitializeComponent();
        }

        //FAP.DataAccess.FAPDataContext context = new FAP.DataAccess.FAPDataContext();
        List<danhmuc> ldm = new List<danhmuc>();
        List<danhmuc> ldmDisplay = new List<danhmuc>();
        List<danhmuc> ldmSelect = new List<danhmuc>();

        bool flag_chb = false;// cho load lại listview
        bool flag_chb1 = false; // cho checkbox all
        bool flag_loadSearch = false;// khi search button khoi chay loadlistview item.checked
        int demItem = 0;

        private void LoadDanhMuc(List<danhmuc> ldmLoad)
        {
            ldmDisplay = new List<danhmuc>();
            danhmuc dm;
            for (int i = 0; i < ldmLoad.Count; i++)
            {
                dm = new danhmuc();
                ListViewItem lvitem = new ListViewItem();
                lvitem.SubItems.Add(ldmLoad[i].tendanhmuc);
                lvitem.SubItems.Add(ldmLoad[i].link);
                lvitem.SubItems.Add(ldmLoad[i].linkdanhmuc);
                lvitem.SubItems.Add(ldmLoad[i].linkpost);
                lvitem.SubItems.Add(ldmLoad[i].tag);
                lvitem.SubItems.Add(ldmLoad[i].id.ToString());
                if (ldmLoad[i].ispost == true)
                {
                    lvitem.SubItems.Add("true");
                }
                else
                {
                    lvitem.SubItems.Add("false");
                }
                lvitem.SubItems.Add(ldmLoad[i].parentid.ToString());
                dm = ldmLoad[i];
                //dm.tendanhmuc = ldmLoad[i].tendanhmuc;
                //dm.id = ldmLoad[i].id;
                //dm.website.link = ldmLoad[i].website.link; // không chạy được nếu có dòng này
                //dm.linkdanhmuc = ldmLoad[i].linkdanhmuc;
                //dm.linkpost = ldmLoad[i].linkpost;
                //dm.parentid = ldmLoad[i].parentid;
                //dm.ispost = ldmLoad[i].ispost;
                //dm.trangthai = ldmLoad[i].trangthai;

                ldmDisplay.Add(dm);

                lvDanhMuc.Items.Add(lvitem);
            }
        }

        private void ucDanhMuc_Load(object sender, EventArgs e)
        {
            try
            {
                var queryAllCustomers = danhmuc.danhmucSelectAll().Where(dm => dm.trangthai == 1 & dm.ispost == true).ToList();// && dm.ispost == true && dm.parentid != 0).ToList();
                List<danhmuc> ldmLoad = queryAllCustomers.ToList();
                LoadDanhMuc(ldmLoad);
                ldm = ldmLoad;
                // danhmuc dmLoad = new danhmuc();
                // dmLoad.website
                //taikhoan tk = new taikhoan();
                //tk.website.
            }
            catch { };
        }

        private void chbCheckAll_CheckedChanged(object sender, EventArgs e)
        {
            if (flag_chb1 == true)
                return;
            flag_chb = true;
            //string search = txtSearch.Text;
            DisplaySelection();
            flag_chb = false;
        }

        private void DisplaySelection()
        {
            if (chbCheckAll.Checked == true)
            {

                for (int i = lvDanhMuc.Items.Count - 1; i >= 0; i--)
                {
                    lvDanhMuc.Items[i].Checked = true;
                    lvDanhMuc.Items[i].BackColor = System.Drawing.Color.Lavender;

                }
                demItem = lvDanhMuc.Items.Count;
                int lvdisplayCount = ldmDisplay.Count;
                List<danhmuc> ldmTemp = new List<danhmuc>(ldmDisplay);
                for (int i = 0; i < lvdisplayCount; i++)
                {
                    for (int j = 0; j < ldmSelect.Count; j++)
                    {
                        if (ldmTemp[i].id == ldmSelect[j].id)
                        {
                            ldmTemp.RemoveAt(i);
                            i--;
                            lvdisplayCount--;
                            break;
                        }
                    }
                }
                ldmSelect.AddRange(ldmTemp);
            }
            else
            {
                for (int i = lvDanhMuc.Items.Count - 1; i >= 0; i--)
                {
                    lvDanhMuc.Items[i].Checked = false;
                    lvDanhMuc.Items[i].BackColor = System.Drawing.Color.White;

                }
                demItem = 0;
                for (int i = 0; i < ldmDisplay.Count; i++)
                {
                    for (int j = 0; j < ldmSelect.Count; j++)
                    {
                        if (ldmDisplay[i].id == ldmSelect[j].id)
                        {
                            ldmSelect.RemoveAt(j);
                            break;
                        }
                    }
                }
            }
        }

        private void lvDanhMuc_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            danhmuc dm = new danhmuc();

            if (flag_chb == true)
                return;
            if (flag_loadSearch == true)
                return;
            int j = lvDanhMuc.Items.Count;
            if (e.Item.Checked == false)
            {
                if (demItem == j)
                {
                    flag_chb1 = true;
                    chbCheckAll.Checked = false;
                    flag_chb1 = false;
                }
                demItem--;
                e.Item.BackColor = System.Drawing.Color.White;

                for (int i = 0; i < ldmSelect.Count; i++)
                {
                    if (e.Item.SubItems[6].Text == ldmSelect[i].id.ToString())
                    {

                        ldmSelect.RemoveAt(i);
                        break;
                    }
                }

            }
            else
            {
                demItem++;
                e.Item.BackColor = System.Drawing.Color.Lavender;
                if (demItem == j)
                {
                    flag_chb1 = true;
                    chbCheckAll.Checked = true;
                    flag_chb1 = false;
                }
                bool flag_selectEdit = false;
                if (ldmSelect != null)
                {
                    for (int i = 0; i < ldmSelect.Count; i++)
                    {
                        if (e.Item.SubItems[6].Text == ldmSelect[i].id.ToString())
                        {
                            flag_selectEdit = true;
                        }
                    }
                }
                if (flag_selectEdit == false)
                {
                    int indexItem = e.Item.Index;
                    dm = ldmDisplay[indexItem];
                    //dm.id = int.Parse(e.Item.SubItems[1].Text);
                    //dm.tendanhmuc = e.Item.SubItems[1].Text;

                    //dm.tendanhmuc = ldmLoad[i].tendanhmuc;
                    //dm.id = ldmLoad[i].id;
                    ////dm.website.link = ldmLoad[i].website.link; // không chạy được nếu có dòng này
                    //dm.linkdanhmuc = ldmLoad[i].linkdanhmuc;
                    //dm.linkpost = ldmLoad[i].linkpost;
                    //dm.parentid = ldmLoad[i].parentid;
                    //dm.ispost = ldmLoad[i].ispost;
                    //dm.trangthai = ldmLoad[i].trangthai;
                    

                    if (ldmSelect == null)
                        ldmSelect = new List<danhmuc>();
                    ldmSelect.Add(dm);
                }

            }
        }

        public delegate void SelectedHandler(List<danhmuc> sender);
        public event SelectedHandler onSelectedDanhMuc;

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (onSelectedDanhMuc != null)
            {
                onSelectedDanhMuc(ldmSelect);
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            var querydanhmuc = danhmuc.danhmucSelectAll().Where(dm => dm.trangthai == 1 & dm.ispost == true).ToList();
            List<danhmuc> ldmbtn = querydanhmuc;
            ldm = querydanhmuc;
            LoadLVDanhSachDanhMuc();

        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void LoadLVDanhSachDanhMuc()
        {
            ldmDisplay = new List<danhmuc>();
            danhmuc dm;
            lvDanhMuc.Items.Clear();
            string search = txtSearch.Text;
            int dem = -1;

            for (int i = 0; i < ldm.Count; i++)
            {
                if (ldm[i].tag != null && ldm[i].tendanhmuc != null && (ldm[i].tag.Contains(search) || ldm[i].tendanhmuc.Contains(search)))
                {
                    ListViewItem lvitem = new ListViewItem();
                    lvitem.SubItems.Add(ldm[i].tendanhmuc);
                    lvitem.SubItems.Add(ldm[i].link);
                    lvitem.SubItems.Add(ldm[i].linkdanhmuc);
                    lvitem.SubItems.Add(ldm[i].linkpost);
                    lvitem.SubItems.Add(ldm[i].tag);
                    lvitem.SubItems.Add(ldm[i].id.ToString());
                    if (ldm[i].ispost == true)
                    {
                        lvitem.SubItems.Add("true");
                    }
                    else
                    {
                        lvitem.SubItems.Add("false");
                    }
                    lvitem.SubItems.Add(ldm[i].parentid.ToString());
                    dm = ldm[i];
                    ldmDisplay.Add(dm);

                    flag_chb = true;
                    lvDanhMuc.Items.Add(lvitem);
                    dem++;
                    flag_chb = false;

                    for (int j = 0; j < ldmSelect.Count; j++)
                    {
                        if (ldm[i].id == ldmSelect[j].id)
                        {
                            lvDanhMuc.Items[dem].Checked = true;
                            break;
                        }
                    }
                }
            }
            demItem = 0;
            for (int i = 0; i < ldmDisplay.Count; i++)
            {
                if (lvDanhMuc.Items[i].Checked == true)
                {
                    demItem++;
                }
            }

            int count = lvDanhMuc.Items.Count;
            if (demItem == count)
            {
                flag_chb1 = true;
                chbCheckAll.Checked = true;
                flag_chb1 = false;
            }
            else
            {
                flag_chb1 = true;
                chbCheckAll.Checked = false;
                flag_chb1 = false;
            }
        }

    }
}
